using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;


namespace PropertyGridEx
{
    [AttributeUsage(AttributeTargets.Property)]
    public class FileNameEditorAttr : Attribute
    {
        private string _initdir;
        private string _filter;

			public string Filter
			{
				get
				{
					return this._filter;
				}
			}

            public string InitialDirectory
            {
                get
                {
                    return this._initdir;
                }
            }
            public FileNameEditorAttr(string filter,string initdir)
			{
				this._filter = filter;
                this._initdir = initdir;
			}

    }

public class UIFileNameEditor : System.Drawing.Design.UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(
                        ITypeDescriptorContext context)
    {
        if (context != null)
            return UITypeEditorEditStyle.Modal;

        return UITypeEditorEditStyle.None;
    }

    [RefreshProperties(RefreshProperties.All)]

    public override object EditValue(ITypeDescriptorContext context,
       IServiceProvider provider, object value)
    {
        FileDialog fd;

        if (context == null || provider == null || context.Instance == null)
            return base.EditValue(provider, value);

        fd = new System.Windows.Forms.OpenFileDialog();
        fd.CheckFileExists = true;
        fd.CheckPathExists = true;
        fd.RestoreDirectory = true;

        fd.Title = "Select Filename";
        fd.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
        fd.Filter = "All Files (*.*)|*.*";

        FileNameEditorAttr fnAttr = (FileNameEditorAttr)context.PropertyDescriptor.Attributes[typeof(FileNameEditorAttr)];

        if (fnAttr!=null)
        {
            fd.Filter = fnAttr.Filter;
            fd.InitialDirectory += "\\" + fnAttr.InitialDirectory;
        }

        if (fd.ShowDialog() == DialogResult.OK)
        {
            if (System.IO.File.Exists(fd.FileName))
            {
                string cwd = System.IO.Directory.GetCurrentDirectory().ToLower();
                string filename=fd.FileName.ToLower();
				filename=(System.IO.Path.GetDirectoryName(filename)+"/"+System.IO.Path.GetFileNameWithoutExtension(filename));
                if (filename.IndexOf(cwd)==0)
                    value = filename.Substring(cwd.Length+1).Replace("\\","/");
            }
        }

        return value;
    }
 }

public class UIFilenameEditor : System.Drawing.Design.UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        if (context != null && context.Instance != null)
        {
            if (!context.PropertyDescriptor.IsReadOnly)
            {
                return UITypeEditorEditStyle.Modal;
            }
        }
        return UITypeEditorEditStyle.None;
    }

    [RefreshProperties(RefreshProperties.All)]
    public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
    {
        if (context == null || provider == null || context.Instance == null)
        {
            return base.EditValue(provider, value);
        }

        FileDialog fileDlg;
        if (context.PropertyDescriptor.Attributes[typeof(SaveFileAttribute)] == null)
        {
            fileDlg = new OpenFileDialog();
        }
        else
        {
            fileDlg = new SaveFileDialog();
        }
        fileDlg.Title = "Select " + context.PropertyDescriptor.DisplayName;
        fileDlg.FileName = (string)value;

        FileDialogFilterAttribute filterAtt = (FileDialogFilterAttribute)context.PropertyDescriptor.Attributes[typeof(FileDialogFilterAttribute)];
        if (filterAtt != null)
        {
            fileDlg.Filter = filterAtt.Filter;
        }
        if (fileDlg.ShowDialog() == DialogResult.OK)
        {
            value = fileDlg.FileName;
        }
        fileDlg.Dispose();
        return value;
    }

    [AttributeUsage(AttributeTargets.Property)]
    public class FileDialogFilterAttribute : Attribute
    {
        private string _filter;

        public string Filter
        {
            get
            {
                return this._filter;
            }
        }

        public FileDialogFilterAttribute(string filter)
        {
            this._filter = filter;
        }
    }

    [AttributeUsage(AttributeTargets.Property)]
    public class SaveFileAttribute : Attribute
    {

    }

    public enum FileDialogType
    {
        LoadFileDialog,
        SaveFileDialog
    }
}

}

 
